﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Admin.Models.ConfigManager;
using Zocono.WCS.Admin.Models.User;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Application.AppTraces;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.Admin.Controllers.ConfigManageMoudle
{
    public class BytesMapObjectInfoController : BaseController
    {
        private readonly PlcReadDbInfoApp _plcReadDbInfoApp;
        private readonly BytesMapObjectInfoApp _bytesMapObjectInfoApp;

        public BytesMapObjectInfoController(PlcReadDbInfoApp plcReadDbInfoApp, BytesMapObjectInfoApp bytesMapObjectInfoApp, IMemoryCache memoryCache, TracesApp tracesApp, IHttpContextAccessor httpContextAccessor) : base(memoryCache, tracesApp, httpContextAccessor)
        {
            this._plcReadDbInfoApp = plcReadDbInfoApp;
            this._bytesMapObjectInfoApp = bytesMapObjectInfoApp;
        }

        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 数据解析界面
        /// </summary>
        /// <returns></returns>
        public ActionResult BytesMapObjectInfoAsync()
        {
            //await AddTracesInfo();
            return View();
        }

        public async Task<JsonResult> GetAllBytesMapObjectInfoAsync()
        {
            var where = new QueryBytesMapObjectInfoDto();
            where.PageIndex = (Start / Length) + 1;
            where.PageSize = Length;
            where.ObjectName = Request.Form["name"].ToString();
            where.EquipmentCode = Request.Form["equipmentCode"].ToString();
            var bytesInfos = await _bytesMapObjectInfoApp.GetBytesMapObjectInfoAsync(where);
            var pageData = new TableData<BytesMapObjectInfoDto>(Draw, bytesInfos.Total, bytesInfos.Total, bytesInfos.Result);
            return Json(pageData);
        }

        /// <summary>
        /// 添加或者编辑数据解析页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ActionResult> BytesMapObjInfoAddOrEditAsync(long id)
        {
            var pageData = new BytesMapObjectInfoPageData();
            var plcReadDatas = await _plcReadDbInfoApp.GetPlcReadDbInfoAsync(new QueryPlcReadDbInfoDto() { PageIndex = 1, PageSize = int.MaxValue });
            pageData.PlcReadDatas = new List<PlcReadPageData>();
            foreach (var plc in plcReadDatas.Result)
            {
                pageData.PlcReadDatas.Add(new PlcReadPageData() { ID = plc.ID.ToString(), ProcessModuleName = plc.ProcessModuleName, PlcName = plc.PlcName }); 
            }
            if (id > 0)
            {
                var bytesMapObjs = await _bytesMapObjectInfoApp.GetBytesMapObjectInfoByIDAsync(id);
                pageData.ID = bytesMapObjs.ID;
                pageData.PlcReadDbId = bytesMapObjs.PlcReadDbId;
                pageData.BytesIndex = bytesMapObjs.BytesIndex;
                pageData.BytesLength = bytesMapObjs.BytesLength;
                pageData.DbCode = bytesMapObjs.DbCode;
                pageData.ObjectCount = bytesMapObjs.ObjectCount;
                pageData.ObjectLength = bytesMapObjs.ObjectLength;
                pageData.ObjectName = bytesMapObjs.ObjectName;
                pageData.ProcessModuleName = bytesMapObjs.ProcessModuleName;
                pageData.ProcessModuleType = bytesMapObjs.ProcessModuleType;
            }

            return View(pageData);
        }

        /// <summary>
        /// 编辑或者添加数据解析
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> OperateBytesMapObjectInfoAsync()
        {
            var bytesMapObjectInfoDto = new BytesMapObjectInfoDto();
            bytesMapObjectInfoDto.ID = Request.Form["id"].ToInt64();
            bytesMapObjectInfoDto.ObjectName = Request.Form["objectName"].ToString();
            bytesMapObjectInfoDto.PlcReadDbId = Request.Form["PlcReadDbId"].ToInt64();
            bytesMapObjectInfoDto.ObjectLength = Request.Form["objectLength"].ToInt32();
            bytesMapObjectInfoDto.BytesIndex = Request.Form["BytesIndex"].ToInt32();
            bytesMapObjectInfoDto.BytesLength = Request.Form["BytesLength"].ToInt32();
            bytesMapObjectInfoDto.EquipmentCode = "0";

            bool result;
            string msg;
            try
            {
                if (bytesMapObjectInfoDto.ID == 0)
                {
                    var res = await _bytesMapObjectInfoApp.AddBytesMapObjectInfoAsync(bytesMapObjectInfoDto);
                    await AddTracesInfo(4, 0, "添加数据解析配置");
                    result = res > 0;
                }
                else
                {
                    result = await _bytesMapObjectInfoApp.UpdateBytesMapObjectInfoAsync(bytesMapObjectInfoDto);
                    await AddTracesInfo(4, 0, "更新数据解析配置");
                }
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }

            return CreateJsonGetResult(result, msg);
        }

        /// <summary>
        /// 删除数据解析
        /// </summary>
        /// <returns></returns>
        public async Task<JsonResult> DelBytesMapObjectInfoAsync()
        {
            var id = Request.Form["id"].ToInt64();
            bool result;
            string msg;
            try
            {
                result = await _bytesMapObjectInfoApp.DeleteBytesMapObjectInfoAsync(id);
                msg = result ? "成功" : "失败";
            }
            catch (Exception e)
            {
                result = false;
                msg = e.Message;
            }
            await AddTracesInfo(4, 0, "删除数据解析配置");
            return CreateJsonGetResult(result, msg);
        }
    }
}
